package net.sudot.chess.business.controller.web;

import net.sudot.chess.exception.BusinessException;
import net.sudot.chess.business.controller.AbstractBaseController;
import net.sudot.chess.business.dto.GameDetailDTO;
import net.sudot.chess.business.model.Category;
import net.sudot.chess.business.model.Game;
import net.sudot.chess.business.service.CategoryService;
import net.sudot.chess.business.service.GameService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 游戏管理控制器
 *
 * @author tangjialin on 2018-01-30.
 */
@Controller
@RequestMapping(value = "/game")
public class GameController extends AbstractBaseController {

    @Resource
    private GameService gameService;
    @Resource
    private CategoryService categoryService;

    /**
     * 游戏分类信息
     *
     * @return 返回游戏分类信息集合
     */
    @PostMapping("/category")
    @ResponseBody
    public List<Category> category() {
        List<Category> categoryList = categoryService.findAll();
        return categoryList;
    }

    /**
     * 游戏信息
     *
     * @param categoryId 游戏分类主键
     * @return 返回游戏信息集合
     */
    @PostMapping("/list/{categoryId}")
    @ResponseBody
    public List<GameDetailDTO> list(@PathVariable("categoryId") Long categoryId) {
        return gameService.findDetailByCategory(categoryId);
    }

    /**
     * 游戏详情视图
     *
     * @param gameId 游戏主键
     * @return 返回游戏详情视图
     */
    @GetMapping("/detail/{gameId}")
    public String detailView(@PathVariable("gameId") Long gameId) {
        return "game/detail";
    }

    /**
     * 游戏详情
     *
     * @param gameId 游戏主键
     * @return 返回游戏详情视图
     */
    @PostMapping("/detail/{gameId}")
    @ResponseBody
    public Game detail(@PathVariable("gameId") Long gameId) {
        Game game = gameService.find(gameId);
        if (game == null) { throw new BusinessException("游戏不存在或已下架"); }
        return game;
    }

}
